package com.usernet.product.action;

import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.usernet.product.comm.BaseAction;
import com.usernet.product.entity.Download;
import com.usernet.product.entity.Software;
import com.usernet.product.tools.DownloadType;
import com.usernet.product.utils.ProductConfig;
import com.usernet.product.utils.ProductUtils;

public class DownAction extends BaseAction {

	// 下载
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("i"));
		String flag = request.getParameter("f");
		Software software = (Software) softwareDao.getById(Software.class, id);
		String userName = request.getParameter("userName");
		software.setCount(software.getCount() + 1);
		if (softwareDao.modify(software)) {
			// 增加下载记录
			Download download = new Download();
			String tel = ProductUtils.getIpAddr(request);
			if (flag == null) {
				if (userName == null) {
					userName = "网站游客";
				}
				download.setType(DownloadType.DOWN_OR_SEA_FROM_WEB);
			} else {
				if (userName == null) {
					userName = "Wap游客";
				}
				download.setType(DownloadType.DOWN_OR_SEA_FROM_WAP);
			}
			download.setSoftName(software.getName());
			download.setUserName(userName);
			download.setTel(tel);

			download.setTime(new Timestamp(System.currentTimeMillis()));
			download.setStatus(0);
			softwareDao.save(download);
		}
		String path = ProductConfig.downPath + "upload/software/"
				+ software.getDownload();
		return new ActionForward(path, true);
	}

}
